package com.um.jdy.mp.commission.manager;

import com.um.jdy.common.platform.code.PlatformCode;
import com.um.jdy.common.platform.entity.po.BankCard;
import com.um.jdy.common.platform.service.BankCardService;
import com.um.jdy.mp.commission.entity.dto.BankCardDTO;
import com.um.jdy.mp.commission.entity.vo.BankCardItemVO;
import com.um.jdy.mp.user.service.SessionUserService;
import com.um.springboot.starter.code.CommonCode;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.orm.params.ListParams;
import com.um.springboot.starter.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class BankCardManager extends BaseManager<BankCardService> {

    @Autowired
    SessionUserService sessionUserService;

    public Response<List<BankCardItemVO>> list(ListParams params) {
        List<BankCardItemVO> cardList = service.getViewModelList("get_my_card_list", BankCardItemVO.class, new Object[]{sessionUserService.getTokenUserId()});
        return Response.success(cardList);
    }

    public Response add(BankCardDTO bankCardDTO) {
        if (StringUtil.isEmpty(bankCardDTO.getBankId())) {
            throw new ServiceException(PlatformCode.NoSelectBank);
        }

        BankCard entity = service.getEntity(new String[]{"card_no", "user_id"}, new Object[]{bankCardDTO.getCardNo(), sessionUserService.getTokenUserId()});
        if (entity != null) {
            throw new ServiceException(PlatformCode.AlreadyIsBank);
        }

        if(!StringUtil.equals(bankCardDTO.getCardName(),sessionUserService.getTokenUser().getLinkman())){
            throw new ServiceException("新增失败：正确户名为"+sessionUserService.getTokenUser().getLinkman());
        }

        BankCard bankCard = new BankCard();
        bankCard.setBaseBankId(bankCardDTO.getBankId());
        bankCard.setAccountOpener(bankCardDTO.getCardName());
        bankCard.setCardNo(bankCardDTO.getCardNo());
        bankCard.setCreateTime(new Date());
        bankCard.setUserId(sessionUserService.getTokenUserId());
        boolean result = service.save(bankCard);
        return result ? Response.success() : Response.failed();
    }

    public Response delete(String bankCardId) {

        checkPermission(bankCardId);

        boolean result = service.delete(bankCardId);
        return result ? Response.success() : Response.failed();
    }

    /**
     * 校验数据权限
     * @param id
     */
    private void checkPermission(String id){
        BankCard entity = service.getEntity(id);
        if(entity == null)
            throw new ServiceException(CommonCode.DataNotExist);

        if(!StringUtil.equals(sessionUserService.getTokenUserId(),entity.getUserId()))
            throw new ServiceException(CommonCode.NoDataPermission);
    }
}
